package com.china08.yxyapi.api.home.activity.marker;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.maker.YxyMakerWorks;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.marker.YxyMakerWorksRepository;

/**
 * 报名列表-下载包
 * 
 */
@CrossOrigin
@RestController
public class WorksZipListDownloadController {

    @Value("${yxy.res.url}")
    private String yxy_res_url;

    @Autowired
    private YxyMakerWorksRepository yxyMakerWorksRepository;

    @RequestMapping(value = "/api_yw/home/marker/works/zipdownload/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws ParseException {
        RespModel model = new RespModel();
        List<String> zipList = new ArrayList<String>();
        List<String> idList = reqModel.ids;
        for (String id : idList) {
            YxyMakerWorks work = yxyMakerWorksRepository.findOne(id);
            // 打包下载url
            String zipurl = yxy_res_url + "/res/downloadHrbMaker/" + work.getId() + "?downloadFilename="
                    + work.getName() + "&registerFilename=" + work.getRegisterFileName() + "&worksFilename="
                    + work.getWorksFileName() + "&worksFileLocation=" + work.getWorksFile() + "&registerFileLocation="
                    + work.getRegisterFile();
            zipList.add(zipurl);
        }
        model.zips = zipList;
        return model;
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public List<String> ids;
    }

    protected static class RespModel {
        // 打包下载url
        public List<String> zips;
    }
}
